#!BPY
"""
Name: 'DGL Model (.dmo)'
Blender: 245
Group: 'Export'
Tooltip: 'DGL Model Exporter'
"""

import Blender
import bpy
from Blender import *

def write_dmo( filepath ):
    editmode = Window.EditMode()
    if editmode:
        Window.EditMode( 0 )
    out = file( filepath, "w" )
    objects = bpy.data.scenes.active.objects
    meshCount = 0
    for ob in objects:
        if ob.type == 'Mesh':
            meshCount = meshCount + 1
    out.write( "Meshes %d\n" % meshCount )
    obIndex = 0
    for ob in objects:
        if( ob.type != 'Mesh' ):
            break
        mesh = ob.getData( mesh = 1 )
        out.write( "\tIndex %d\n" % obIndex )
        if ob.parent != None:
            out.write( "\tHasParent 1\n" )
        else:
            out.write( "\tHasParent 0\n" )
        out.write( "\tHasFaceUV %d\n" % mesh.faceUV )
        ob = objects[obIndex]
        out.write( "\tName %s\n" % ob.name )
        if ob.parent != None:
            out.write( "\tParentName %s\n" % ob.parent.name )
        for i in range( 4 ):
            out.write( "\tMatrixLocalLine%d " % i )
            for j in range( 4 ):
                out.write( "%f " % ob.matrixLocal[i][j] )
            out.write( "\n" )
        colors = {}
        uvs = {}
        faces = []
        faceCount = 0
        for face in mesh.faces:
            faces.append( face );
            if len( face ) == 4:
                faceCount = faceCount + 2
            else:
                faceCount = faceCount + 1
            for vertInFace in range( len( face ) ):
                vertIndex = face.verts[vertInFace].index
                if mesh.vertexColors == 0:
                    mesh.vertexColors = 1
                colors[vertIndex] = face.col[vertInFace]
                if mesh.faceUV == 1:
                    uvs[vertIndex] = face.uv[vertInFace]
        out.write( "\tVertices %d\n" % len( mesh.verts ) )
        for i in range( len( mesh.verts ) ):
            out.write( "\t\tIndex %d\n" % mesh.verts[i].index )
            out.write( "\t\tPosition %f %f %f\n" % ( mesh.verts[i].co.x, mesh.verts[i].co.y, mesh.verts[i].co.z ) )
            out.write( "\t\tNormal %f %f %f\n" % ( mesh.verts[i].no.x, mesh.verts[i].no.y, mesh.verts[i].no.z ) )
            out.write( "\t\tColor %d %d %d %d\n" % ( colors[i].r, colors[i].g, colors[i].b, colors[i].a ) )
            if mesh.faceUV == 1:
                out.write( "\t\tTexCoord %f %f\n" % ( uvs[i].x, uvs[i].y ) )
        out.write( "\tFaces %d\n" % faceCount )
        faceIndex = 0
        for face in faces:
            if len( face ) == 3:
                out.write( "\t\tIndex %d\n" % faceIndex )
                if mesh.faceUV == 1 and face.image != None:
                    out.write( "\t\tHasTexture 1\n" )
                else:
                    out.write( "\t\tHasTexture 0\n" )
                out.write( "\t\tVertexIndex %d %d %d\n" % ( face.verts[0].index, face.verts[1].index, face.verts[2].index ) )
                if mesh.faceUV == 1 and face.image != None:
                    out.write( "\t\tTexture %s\n" % face.image.filename )
                faceIndex = faceIndex + 1
            else:
                out.write( "\t\tIndex %d\n" % faceIndex )
                if mesh.faceUV == 1 and face.image != None:
                    out.write( "\t\tHasTexture 1\n" )
                else:
                    out.write( "\t\tHasTexture 0\n" )
                out.write( "\t\tVertexIndex %d %d %d\n" % ( face.verts[0].index, face.verts[1].index, face.verts[2].index ) )
                if mesh.faceUV == 1 and face.image != None:
                    out.write( "\t\tTexture %s\n" % face.image.filename )
                faceIndex = faceIndex + 1

                out.write( "\t\tIndex %d\n" % faceIndex )
                if mesh.faceUV == 1 and face.image != None:
                    out.write( "\t\tHasTexture 1\n" )
                else:
                    out.write( "\t\tHasTexture 0\n" )
                out.write( "\t\tVertexIndex %d %d %d\n" % ( face.verts[0].index, face.verts[2].index, face.verts[3].index ) )
                if mesh.faceUV == 1 and face.image != None:
                    out.write( "\t\tTexture %s\n" % face.image.filename )
                faceIndex = faceIndex + 1
        obIndex = obIndex + 1
    out.close()
    if editmode:
        Window.EditMode( 1 )

Blender.Window.FileSelector( write_dmo, "Export", Blender.sys.makename( ext = '.dmo' ) )